{extend name="app/admin/view/base.html"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>PC端首页导航设置</li>
		</ul>
	</div>
</div>
<div class="layui-form ns-form">
	<input type="hidden" name="id" value="{$nav_info.id}">
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>导航名称：</label>
		<div class="layui-input-block">
			<input name="nav_title" type="text" lay-verify="required" class="layui-input ns-len-mid" {if $nav_info} value="{$nav_info.nav_title}" {/if}>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>链接地址：</label>
		<div class="layui-input-block ns-len-mid">
			<select name="link" lay-filter="link">
				<option value="">请选择</option>
				{foreach $link as $k => $v}
				<option value="{$v.url}" {if $nav_info && $nav_info.nav_url && json_decode($nav_info.nav_url,true)['title'] == $v.title}selected{/if}>{$v.title}</option>
				{/foreach}
				<option value="diy" {if $nav_info && $nav_info.nav_url && json_decode($nav_info.nav_url,true)['title'] == '自定义'}selected{/if}>自定义</option>
			</select>
			<input name="nav_url" type="hidden" {if $nav_info} value="{$nav_info.nav_url}" {/if}>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">是否显示：</label>
		<div class="layui-input-block">
			<input type="checkbox" name="is_show" value="1" lay-skin="switch" {if condition="$nav_info && $nav_info.is_show == 1"} checked {/if}  />
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">是否新窗口打开：</label>
		<div class="layui-input-block">
			<input type="checkbox" name="is_blank" value="1" lay-skin="switch" {if condition="$nav_info && $nav_info.is_blank == 1"} checked {/if} />
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required"></span>排序：</label>
		<div class="layui-input-block">
			<input name="sort" type="text"  class="layui-input ns-len-short" {if $nav_info} value="{$nav_info.sort}"{else /}value="0" {/if}>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">导航图标：</label>
		<div class="layui-input-block">
			<div class="upload-img-block img-upload">
				<div class="upload-img-box {notempty name="$nav_info['nav_icon']"}hover{/notempty}">
					<div class="ns-upload-default" id="imgUpload">
						{if condition="$nav_info.nav_icon"}
						<div id="preview_imgUpload" class="preview_img">
							<img layer-src src="{:img($nav_info.nav_icon)}" class="img_prev"/>
						</div>
						{else/}
						<div class="upload">
							<img src="ADMIN_IMG/upload_img.png"/>
							<p>点击上传</p>
						</div>
						{/if}
					</div>
					<div class="operation">
						<div>
							<i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
							<i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
						</div>
						<div class="replace_img js-replace">点击替换</div>
					</div>
					<input type="hidden" name="nav_icon" value="{$nav_info.nav_icon}">
				</div>
			</div>
		</div>
	</div>

	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
	</div>

</div>
{/block}

{block name="script"}
<script>
	layui.use(['form'], function() {
		var form = layui.form,repeat_flag=false;
		form.on('select(link)', function(data){
			var title = $(data.elem).find("option:selected").text();
			if(data.value != 'diy'){
				$("input[name='nav_url']").val(JSON.stringify({
					title : title,
					url:data.value
				}));
			}else{
				var value = $("input[name='nav_url']").val();
				if(value) value = JSON.parse(value).url;
				layer.prompt({
					formType: 2,
					value :value,
					title: '自定义链接地址',
					area: ['450px', '100px'],
					cancel : function () {
						$("input[name='nav_url']").val("");
					}
				}, function(value, index, elem){
					$("input[name='nav_url']").val(JSON.stringify({
						title : title,
						url:value
					}));
					layer.close(index);
				});
			}
		});

		// 普通图片上传
		var logo_upload = new Upload({
			elem: '#imgUpload'
		});

		form.on('submit(save)', function(data) {
			if(!data.field.nav_url){
				layer.msg("请输入链接地址");
				return;
			}
			if (repeat_flag) return;
			repeat_flag = true;
			
			//图片删除
			if(!data.field.nav_icon) logo_upload.delete();
			
			$.ajax({
				url: ns.url("pc://admin/pc/editNav"),
				data: data.field,
				dataType: 'JSON',
				type: 'POST',
				success: function(res) {
					layer.msg(res.message);
					if (res.code == 0) {
						location.href = ns.url("pc://admin/pc/navlist");
					}
					repeat_flag = false;
				}
			});
		});
	});
</script>
{/block}